<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.search" size="default" placeholder="请输入菜品分类名字" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="getDishAll()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddRole()">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增分类
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="id" label="序号" width="60" />
		
				<el-table-column prop="nameType" label="分类名称" show-overflow-tooltip></el-table-column>

				<el-table-column prop="sort" label="排序" show-overflow-tooltip> </el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditRole(scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="deleteDish(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</div>

		<!-- <div class="system-role-dialog-container">
			<el-dialog :title="statses.dialog.title" v-model="statses.dialog.load" width="769px">
				<el-form ref="roleDialogFormRef" :model="statses.ruleForm" size="default" label-width="90px">
					<el-row :gutter="35">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="分类名称:">
								<el-input v-model="statses.ruleForm.nameType" placeholder="请输入分类名称12" clearable></el-input>
							</el-form-item>
						</el-col>


						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="排序::">
								<el-input v-model="statses.ruleForm.sort" placeholder="请输入排序" clearable></el-input>
							</el-form-item>
						</el-col>

					
					</el-row>
				</el-form>
				<template #footer>
					<span class="dialog-footer">
						<el-button @click="caner()" size="default">取 消</el-button>
						<el-button type="primary" @click="updateDish()" size="default">{{ statses.dialog.updatatTxt }}</el-button>
					</span>
				</template>
			</el-dialog>
		</div> -->

		<div class="system-role-dialog-container">
			<el-dialog :title="statses.dialog.title" v-model="statses.dialog.load" width="769px">
				<el-form ref="roleDialogFormRef" :model="statses.ruleForm" size="default" label-width="90px">
					<el-row :gutter="35">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="分类名称:">
								<el-input v-model="statses.ruleForm.nameType" placeholder="请输入菜品分类" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="排序::">
								<el-input v-model="statses.ruleForm.sort" placeholder="请输入排序" clearable></el-input>
							</el-form-item>
						</el-col>

					

					</el-row>
				</el-form>
				<template #footer>
					<span class="dialog-footer">
						<el-button @click="caner()" size="default">取 消</el-button>
						<el-button type="primary" @click="add()" size="default">新增</el-button>
					</span>
				</template>
			</el-dialog>
		</div>
	</div>
</template>

<script setup lang="ts" name="systemRole">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDishtypeApi } from '/@/api/dish/dishtype';
import { number } from 'echarts';


// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/system/role/dialog.vue'));

// 定义变量内容
const roleDialogRef = ref();
const state = reactive<SysRoleState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			search: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});

const statses = reactive({
	ruleForm: {
		nameType: '', // 菜品名称
		sort: NaN, // 排序
	},
	dialog: {
		load: false,
		laoding: true,
		isShowDialog: false,
		type: '',
		title: '新增优惠券',
		submitTxt: '新增',
		updatatTxt: '修改',
	},
});

//获取token
let tes = JSON.parse(localStorage.getItem('user') || '{}');
const customHeaders = reactive({
	token: tes.data,
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	const data = [];
	for (let i = 0; i < 20; i++) {
		data.push({
			roleName: i === 0 ? '超级管理员' : '普通用户',
			roleSign: i === 0 ? 'admin' : 'common',
			describe: `测试角色${i + 1}`,
			sort: i,
			status: true,
			createTime: new Date().toLocaleString(),
		});
	}
	state.tableData.data = data;
	state.tableData.total = state.tableData.data.length;
	setTimeout(() => {
		state.tableData.loading = false;
	}, 500);
};
//获取菜品信息
const getDishAll = async () => {
	useDishtypeApi()
		.getDishAll({ name: state.tableData.param.search, pageNum: state.tableData.param.pageNum, pageSize: state.tableData.param.pageSize })
		.then((res) => {
			state.tableData.data = res.data.records;
			state.tableData.total = res.data.total;

		});
};

//图片上传处理
const handleAvatarSuccess = (res: any, file: any) => {
	statses.ruleForm.photograph = res.data;
};
const beforeAvatarUpload = () => {};

//修改菜品信息
const updateDish = async () => {
	console.log(statses.ruleForm);
	useDishtypeApi()
		.update(statses.ruleForm)
		.then(() => {
			caner();
		});
		getDishAll();

};
//清空属性值
const caner = () => {
	statses.ruleForm = {
		nameType: '', // 优惠券名称
		sort: NaN, // 排序
	
	};
	statses.dialog.load = false;
};
//新增菜品信息
const add = async () => {
	console.log(statses.ruleForm);
	
	useDishtypeApi()
		.add(statses.ruleForm)
		.then(() => {
			caner();
		});
		getDishAll();
};
//删除菜品信息
const deleteDish = (row: any) => {
	console.log(row);
	useDishtypeApi()
		.delete(row)
		.then((res) => {
			getDishAll();
		});
		getDishAll();
};

//确保优惠额度大于零且保留两位小数
const validate = (val: number) => {
	if (val < 0) {
		ElMessage.error('输入优惠额度必须是数字且大于零，小数只保留两位小数');
		statses.ruleForm.sort = 0;
	} else {
		statses.ruleForm.price = Math.round(val * 100) / 100;
	}
};

// 打开新增菜品窗口
const onOpenAddRole = () => {
	caner();
	statses.dialog.load = true;
};
// 打开修改弹窗
const onOpenEditRole = (row: any) => {
	console.log(row);
	statses.dialog.load = true;
	statses.ruleForm = row;
};
// 删除角色
const onRowDel = (row: RowRoleType) => {
	ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.roleName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			getTableData();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getDishAll();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getDishAll();
};
// 页面加载时
onMounted(() => {
	getDishAll();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;

		.el-table {
			flex: 1;
		}
	}
}

.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}

.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}

.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}

.avatar {
	width: 178px;
	height: 178px;
	display: block;
}

.box {
	border: 5px #8c939d; /* 边框宽度、样式和颜色 */
	border-radius: 10px; /* 可选：圆角边框 */
	padding: 10px; /* 可选：内边距 */
	box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
}
</style>
